package com.cxs.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cxs.entity.dto.LoginDTO;
import com.cxs.entity.Admin;
import com.cxs.entity.Student;
import com.cxs.service.AdminService;
import com.cxs.service.ClazzService;
import com.cxs.service.StudentService;
import com.cxs.entity.bo.ClazzBo;
import com.cxs.entity.vo.AdminLoginVO;
import com.cxs.entity.vo.StudentLoginVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;

/*
 * @Project:seat-subscribe-sys
 * @Author:cxs
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
@Controller
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private AdminService adminService;

    @Autowired
    private ClazzService clazzService;

    @RequestMapping("/login")
    public ModelAndView authLogin(ModelAndView mv, HttpServletRequest request){
        mv.setViewName("login");
        return mv;
    }

    @RequestMapping("/executeLogin")
    public String authExecuteLogin(LoginDTO req, Model mv, HttpServletRequest request){
        HttpSession session = request.getSession();
        if (!authExecuteLoginCheck(req, mv)) {
            return "forward:/auth/login";
        }
        Integer type = req.getType();
        if (type == 1) {
            // 管理员
            LambdaQueryWrapper<Admin> adminLambdaQueryWrapper = new LambdaQueryWrapper<>();
            adminLambdaQueryWrapper.eq(Admin::getUsername, req.getUsername())
                    .eq(Admin::getPassword, req.getPassword());
            Admin admin = adminService.getOne(adminLambdaQueryWrapper);
            if (null == admin) {
                mv.addAttribute("errorMsg", "用户名或密码错误");
                return "forward:/auth/login";
            }
            AdminLoginVO vo = new AdminLoginVO();
            BeanUtils.copyProperties(admin, vo);
            vo.setRole("管理员");
            session.setAttribute("loginUser", vo);
            return "index";
        } else {
            // 学生
            LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
            studentLambdaQueryWrapper.eq(Student::getSno, Integer.parseInt(req.getUsername()))
                    .eq(Student::getPassword, req.getPassword());
            Student student = studentService.getOne(studentLambdaQueryWrapper);
            if (null == student) {
                mv.addAttribute("errorMsg", "用户名或密码错误");
                return "forward:/auth/login";
            }
            StudentLoginVO vo = new StudentLoginVO();
            BeanUtils.copyProperties(student, vo);
            vo.setRole("学生");

            ClazzBo clazzBo = clazzService.getClazzInfoByClazzId(student.getClazzId());
            if (null != clazzBo) {
                vo.setGradeName(clazzBo.getGradeName());
                vo.setClazzName(clazzBo.getClazzName());
            }
            session.setAttribute("loginUser", vo);
            return "index";
        }
    }

    @RequestMapping("/logout")
    public ModelAndView logout(ModelAndView mv, HttpSession session){
        Object loginUser = session.getAttribute("loginUser");
        if (null != loginUser) {
            session.invalidate();
        }
        mv.setViewName("login");
        return mv;
    }

    private boolean authExecuteLoginCheck(LoginDTO req, Model mv) {
        Integer type = req.getType();
        String username = req.getUsername();
        String password = req.getPassword();
        if (null == type || !Arrays.asList(1,2).contains(type)) {
            mv.addAttribute("errorMsg", "登录类型必传且只能为[1,2]");
            return false;
        }

        if (!StringUtils.hasLength(password)) {
            mv.addAttribute("errorMsg", "登录密码岂能为空");
            return false;
        }
        if (type == 1) {
            if (!StringUtils.hasLength(username)) {
                mv.addAttribute("errorMsg", "管理员用户名岂能为空");
                return false;
            }
        } else {
            if (!StringUtils.hasLength(username)) {
                mv.addAttribute("errorMsg", "学生学号岂能为空");
                return false;
            } else {
                try {
                    int i = Integer.parseInt(username);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                    mv.addAttribute("errorMsg", "学生学号格式错误");
                    return false;
                }
            }
        }
        return true;
    }
}
